from cStringIO import StringIO
import radix
import MySQLdb
import re
import GeoIP

r = radix.Radix()
doc_string = open('PrefixToAS.map').read()
doc = StringIO(doc_string)
for line in doc:
        arr = line.split('|')
        node = r.add(arr[1])
        node.data.update({'as':arr[2]})

conn = MySQLdb.connect('localhost', 'root', '48765382', 'test')
conn.set_character_set('utf8')
cursor = conn.cursor()

doc_string = open('20100101.txt').read()
doc = StringIO(doc_string)

gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)

reip = re.compile(r'(\d{1,3}\.){3}\d{1,3}')

for line in doc:
	try:
		ip = re.search(reip, line).group()
		country = gi.country_code_by_addr(ip)
        	if country == None or country != 'CN':
			continue
		node = r.search_best(ip)
		if node != None:
			asn = node.data['as']
			if asn != None:
				sql = 'insert ignore into 300ip values("' + ip + '", "' + asn + '")'
				cursor.execute(sql)
	except:
		print line

cursor.close()
conn.close()			
